package edu.tamu.cs.studio;

import java.util.*;
/*********************************
updated Time: 6/22/2012 :: 3:00p.m
person updated: Larry Powell
what changes made to code: 
Cleaned the code and add the function corner:
add code to the chooseMove.
********************************/



public class AIengine
{

	
	public char color;
	public int row;
	public int column;
	public AIengine()
	{
		row = 0;
		column = 0;
	}
	



	public char return_color()
{
	return color;
}

	public void get_color(char c)
{
	color=c;
}

	public int count_num_moves(Gameboard board)
{
	int p = 0;

	for(int i=1; i<10; i++)
	{
		for(int j=1; j<10; j++)
		{
			if(board.board[i][j] == '*')
			{
				p++;
			}
		}
	}
	return p;

}

	public void think(Gameboard board,int level)
{

	int row1,column1;
	int newmovevalue;
	int bestmove=100;
	for(int i=0; i<10; i++)
	{
		for(int j=0; j<10; j++)
		{
			if(board.board[i][j] == '*')
			{
				row1 = i;
				column1 = j;
				newmovevalue = chooseMove(2,board,row1,column1,level);
				if(bestmove >= newmovevalue)
				{
					bestmove = newmovevalue;
					row = row1;
					column = column1;
				}
			}
		}
	}
}

	public void  think_random(Gameboard board)
{
/*************************************************
	Get number of available moves from board
	Choose random number from that value
	When AI gets to that random number from legal moves 
	iteration set row and column
*************************************************/
	//srand (time(0) );
		Random generator = new Random();
	int r;
	if(count_num_moves(board) != 0)		//avoid arithmetic exception with rand() by avoiding dividing by 0
		r = (/*rand()*/generator.nextInt( ) % count_num_moves(board));
	else
		r = 0;
		
	int num_times_through = 0;
	for(int i=0; i<10; i++)
	{
		for(int j=0; j<10; j++)
		{
			if(board.board[i][j] == '*')
			{
				if(r == num_times_through)	//checks if iteration# matches random number chosen earlier
				{
				row = i;
				column = j;
				}
				num_times_through++;
			}
		}
	}
}

	public int chooseMove( int depth, Gameboard board, int row1, int column1, int level)
{

	int bestoption=-1;
	int color_count=0;
	int space_count=0;
	if(depth < level&&!no_move_available(board))
	{
		int d=depth+1;
		int bestoptionhigh=-1;
		int bestoptionlow=100;
		int l = row1;
		int k = column1;
		int newmover;
		board.move(l,k);
		board.showNextPos();
		for(int i=0; i<10; i++)
		{
			for(int j=0; j<10; j++)
			{
				if(board.board[i][j] == '*')
				{
					if((i == 1 && j == 1) || (i == 1 && j == 8) || (i == 8 && j == 1) || (i == 8 && j == 8) && (depth%2 == 0))
					{
						newmover = 100;
					}
					else
						newmover = chooseMove(d,board,i,j,level);
					
					if((bestoptionhigh <= newmover) && ((depth)%2 == 0))
					{
						bestoption = newmover;
					}
					if((bestoptionlow >= newmover) && ((depth)%2 != 0))
					{
						bestoption = newmover;
					}
				}
			}
		}
		return bestoption;
	}
	else
	{
		if(corner(board) && (depth == level))
		{
			return 100;
		}
		if(depth==level&&!no_move_available(board)&&!gameover(board))
		{
			bestoption = count_num_moves(board);
			return bestoption;
		}
		if(no_move_available(board)&&!gameover(board))
		{
			return 100;
		}
		if(gameover(board))
		{
			for(int i=0; i<10; i++)
			{
				for(int j=0; j<10; j++)
				{
					if(board.board[i][j] == color)
					{
						color_count++;
					}
					if(board.board[i][j] =='_')
					{
						space_count++;
					}
				}
			}
			if(64-(color_count+space_count)<color_count)
			{
				return -1;
			}
			else
			{
				return 100;
			}
		}
	}
	return 0;//this might break the code
}
 
public boolean corner(Gameboard board)
{
	for(int i=0; i<10; i++)
	{
		for(int j=0; j<10; j++)
		{
			if(board.board[i][j] == '*')
			{
				if((i == 1 && j == 1) || (i == 1 && j == 8) || (i == 8 && j == 1) || (i == 8 && j == 8) )
				{
					return true;
				}
			}
		}
	}
	return false;
}
public boolean no_move_available(Gameboard board)
{
	if(count_num_moves(board)==0)
	return true;
	return false;
}

public boolean gameover(Gameboard board)
{
	int numEmpty=0;
	int p=0;
	for(int i=0; i<10; i++)
	{
		for(int j=0; j<10; j++)
		{
			if(board.board[i][j] == '_')
			{
				numEmpty++;
			}
			if(board.board[i][j] == '*')
			{
				p++;
			}
		}
	}
	if((p == 0 )&& numEmpty==0)
	return true;
	return false;

}
public void difficulty_of_the_AI(char a, Gameboard board)
{
	switch (a)
	{
		case '1':
			think_random(board);
			break;
		case '2':
			think(board,4);
			break;
		case '3':
			think(board,6);
			break;
		default:
			//cout << "Sorry thats not an option" << endl;
			break;
	}
}
};


